package com.dmp.pushlogs.bolt;

import com.dmp.common.date.DateUtils;
import com.dmp.storm.bolt.BaseMapBolt;
import eu.bitwalker.useragentutils.DeviceType;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Map;


/**
 * @ClassName CustomBolt
 * @Description 本地化字段处理
 *
 * @author sunzy
 * @date 2016年4月14日 下午4:37:42 
 * @version 1.0 
 * 
 */
public class GetCustomBolt extends BaseMapBolt {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3372732981391364586L;
	private String fieldFrom;
	private String fieldTo;

	@Override
	public void prepare() {
		if (fieldTo == null) {
			fieldTo = fieldFrom;
		}
	}

	@Override
	public Map<String, Object> process(Map<String, Object> map) {
       Map<String, Object> record = map;
		
		// start_time 毫秒-> yyyyMMddHHmmss
		String timeStr = (String) map.get("get_time");	
		long timeL = Long.valueOf(timeStr).longValue();
		Date date = new Date(timeL);
		timeStr = DateUtils.formatDate(date, "yyyyMMddHHmmss");
		record.put("get_time", timeStr);
		
		// 其它时间
		record.put("hour", DateUtils.formatDate(date, "yyyyMMddHH"));
		record.put("hour_h", DateUtils.formatDate(date, "HH"));
		record.put("day", DateUtils.formatDate(date, "yyyyMMdd"));
		record.put("day_d", DateUtils.formatDate(date, "dd"));
		record.put("month", DateUtils.formatDate(date, "yyyyMM"));
		record.put("month_m", DateUtils.formatDate(date, "MM"));
		//日志时间
		record.put("log_date", DateUtils.formatDate(date, "yyyy/MM/dd HH:mm:ss"));
		return record;
	}
	
	public static void main(String...args){
		long adpushTimeL = System.currentTimeMillis();
		Date adpushDate = new Date(adpushTimeL);
		System.out.println(DateUtils.formatDate(adpushDate, "yyyyMMddHHmmss"));
//		String logTime = "01/Oct/2016:04:13:23 +0800";
//		try {
//			Date logDate = DateUtils.parseDate(logTime, Locale.ENGLISH,
//					"dd/MMM/yyyy:HH:mm:ss Z");
//			logTime = DateUtils.formatDate(logDate, "yyyyMMddHHmmss");
//			System.out.println(logTime);
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
	}

	private String parseUserAgent(String userAgentString) {
		String platformType = "other";

		if (StringUtils.isBlank(userAgentString))
			return platformType;

		UserAgent userAgent = UserAgent.parseUserAgentString(userAgentString);

		if (DeviceType.COMPUTER.equals(userAgent.getOperatingSystem()
				.getDeviceType())) {
			platformType = "pc";
		} else if (StringUtils.containsIgnoreCase(userAgentString, "android")) {
			platformType = "android";
		} else if (StringUtils.containsIgnoreCase(userAgentString, "iphone")) {
			platformType = "apple";
		}

		return platformType;
	}

	public void setFieldFrom(String fieldFrom) {
		this.fieldFrom = fieldFrom;
	}

	public void setFieldTo(String fieldTo) {
		this.fieldTo = fieldTo;
	}

}
